﻿using System;
using System.Collections.Generic;
using System.Text;

namespace KCLIMTrading.Mgmt
{
	public class PurchaseOrder
	{
		#region Variable
		private String number;
		private DateTime deliver;
		private DateTime cancelation;
		//private String fileName;
		#endregion

		#region Properties
		private int id;
		/// <summary>
		/// Gets or sets Id for PO.
		/// </summary>
		/// <remarks>
		/// This is used by customer unique PO id at backend processing.
		/// </remarks>
		public int Id { get { return this.id; } set { this.id = value; } }
		/// <summary>
		/// Set or get Purchase Order Code
		/// </summary>
		public String Number
		{
			get
			{
				return this.number;
			}

			set
			{
				this.number = value;
			}
		}

		private Customer soldTo;
		/// <summary>
		/// Gets or sets sold to customer.
		/// </summary>
		public Customer SoldTo { get { return this.soldTo; } set { this.soldTo = value; } }
		/// <summary>
		/// Set or get delivery date
		/// </summary>
		public DateTime DeliveryDate
		{
			get
			{
				return this.deliver;
			}

			set
			{
				this.deliver = value;
			}

		}
		/// <summary>
		/// Set or get cancelation date
		/// </summary>
		public DateTime CancelationDate
		{
			get
			{
				return this.cancelation;
			}

			set
			{
				this.cancelation = value;
			}
		}

		private List<Item> items;
		/// <summary>
		/// Gets or sets a collection of items.
		/// </summary>
		public List<Item> Items { get { return this.items; } set { this.items = value; } }
		/// <summary>
		/// Return total amount of all items.
		/// </summary>
		public decimal Total
		{
			get
			{
				decimal total = 0m;
				foreach(Item item in this.items)
					total += item.NetValue;
				
				return total;
			}
		}
		#endregion

		/// <summary>
		/// Default constructor.
		/// </summary>
		public PurchaseOrder()
		{
			this.id = 0;
			this.number = string.Empty;
			this.deliver = DateTime.MinValue;
			this.cancelation = DateTime.MinValue;
			this.items = new List<Item>();
			this.soldTo = new Customer();
		}
		/// <summary>
		/// Constructor for retrieve.
		/// </summary>
		/// <param name="id"></param>
		public PurchaseOrder(int id)
		{
			this.id = 0;
			this.number = string.Empty;
			this.deliver = DateTime.MinValue;
			this.cancelation = DateTime.MinValue;
			this.items = new List<Item>();
			this.soldTo = new Customer();
			
			Open();
		}
		/// <summary>
		/// Override ToString output.
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			string display = string.Empty;
			if(this.id > 0) display += this.id.ToString();
			if(this.number.Length>0)
			{
				if(display.Length>0) display += ": ";
				display += this.number;
			}
			
			return display;
		}
		/// <summary>
		/// TODO: Open PO based on id or PO Number to retrieve the datastore file.
		/// </summary>
		/// <returns></returns>
		public bool Open()
		{
			//this.id;
			//this.number;
			throw new NotImplementedException();
		}
		/// <summary>
		/// TODO: Save this PO to local path.
		/// </summary>
		/// <returns></returns>
		public bool Save()
		{
			throw new NotImplementedException();
		}
	}
}